﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

using WpfApp4.Models;
using WpfApp4.Models.Config;

namespace WpfApp4.Utilities
{
    public static class Utility
    {
        public static WindowSize GetWindowSize(string url)
        {
            var menu = GetMenu(MenuInfo.Instance.MenuItems, url);
            if (menu != null)
            {
                var windowSize = menu.WindowSize;
                if (!string.IsNullOrEmpty(windowSize))
                {
                    var width = Convert.ToInt32(windowSize.Split('*')[0].Trim());
                    var height = Convert.ToInt32(windowSize.Split('*')[1].Trim());
                    return new WindowSize(width, height);
                }
            }
            return new WindowSize(ApplicationConfigSetting.DefaultWidth, ApplicationConfigSetting.DefaultHeight);
        }

        public static WindowSize GetWindowSize(MenuConfig config)
        {
            if (!string.IsNullOrWhiteSpace(config.WindowSize))
            {
                var width = Convert.ToInt32(config.WindowSize.Split('*')[0].Trim());
                var height = Convert.ToInt32(config.WindowSize.Split('*')[1].Trim());
                return new WindowSize(width, height);
            }
            return new WindowSize(ApplicationConfigSetting.DefaultWidth, ApplicationConfigSetting.DefaultHeight);
        }

        public static WindowSize ConvertWindowSize(string size)
        {
            try
            {
                var width = Convert.ToInt32(size.Split('*')[0].Trim());
                var height = Convert.ToInt32(size.Split('*')[1].Trim());
                return new WindowSize(width, height);
            }
            catch
            {
                return new WindowSize(ApplicationConfigSetting.DefaultWidth, ApplicationConfigSetting.DefaultHeight);
            }
        }

        private static MenuConfig GetMenu(MenuConfig[] menus, string url)
        {
            foreach (var item in menus)
            {
                if (item.CommandParameter.Equals(url))
                {
                    return item;
                }
                if (item.MenuItems != null && item.MenuItems.Length > 0)
                {
                    var res = GetMenu(item.MenuItems, url);
                    if (res != null)
                    {
                        return res;
                    }
                }
            }
            return null;
        }


        public static MenuConfig GetMenuByName(string name)
        {
            var menu = GetMenuByName(MenuInfo.Instance.MenuItems, name);
            if(menu == null)
            {
                var list = MenuInfo.Instance.MenuItems.ToList();
                list.Insert(list.Count - 1, new MenuConfig { Name = "已打开窗口", MenuItems = new MenuConfig[] { } });
                MenuInfo.Instance.MenuItems = list.ToArray();

                return MenuInfo.Instance.MenuItems[MenuInfo.Instance.MenuItems.Length - 2];
            }
            return menu;
        }

        private static MenuConfig GetMenuByName(MenuConfig[] menus, string name)
        {
            foreach (var item in menus)
            {
                if (item.Name.Equals(name))
                {
                    return item;
                }
                if (item.MenuItems != null && item.MenuItems.Length > 0)
                {
                    var res = GetMenuByName(item.MenuItems, name);
                    if (res != null)
                    {
                        return res;
                    }
                }
            }
            return null;
        }

        public static void CollectUrl(string url)
        {
            var list = MenuInfo.Instance.MenuItems.ToList();
            MenuConfig tool = null;

            bool find = false;
            while (!find)
            {
                foreach (var item in list)
                {
                    if (item.Name == "工具箱")
                    {
                        find = true;
                        tool = item;
                        break;
                    }
                }

            }
        }

        private static void FindTool(MenuConfig[] menus)
        {
            foreach (var item in menus)
            {
                if (item.Name == "工具箱")
                {
                    return;
                }
            }
        }
    }
}
